/**
 * 登陆页面JS
 * Created by 杨浩宇 on 2016/8/1 0004.
 */
define(["require", "jquery", "public/app","template"], function (require, $, app,template) {
    function Login($container) {
        this._$container = $container;
        this._init();
    };

    Login.prototype._init = function () {
        this._initUrlPath();
        this._initComponents();
        this._initEventBinding();
    };

    Login.prototype._initUrlPath = function () {
        this.urlPaths = {
            login: app.urlBuild("/login"),
            homePage: app.urlBuild("/pages/index.jsp")
        }
    };

    Login.prototype._initComponents = function () {
        var data = {
            companyName:'四川创立信息科技有限责任公司',
            copyright:'2016-2017',
            phone:'18615795736'
        };
        var html = template('build_login_foot',data);
        $('#login_foot').html(html);
    };

    Login.prototype._initEventBinding = function () {
        this._$container.find("#loginBtn").on("click", {self: this}, this.actions.login);
        $(document).keyup(function (event) { //页面上点击回车，提交登录
            var theEvent = window.event || event;
            var code = theEvent.keyCode || theEvent.which;
            if (code == 13) {
                $("#loginBtn").click();
            }
        });
    }

    Login.prototype.actions = {
        login: function (evt) {
            var self = evt.data.self;
            var data = app.utils.formToJSON($("#loginForm"));
            data.password = $("#password").val();
            var options = {
                url: self.urlPaths.login+"?"+$.param(data),
                type: "post",
                success: function (data) {
                    app.setUserPrincipal(data);
                    window.location = self.urlPaths.homePage;
                }
            };

            $.ajax(options);
        }
    };

    return Login;
});